package dem1;

import java.util.*;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 33013
 * Date: 2023-05-29
 * Time: 10:01
 */

class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}

class Solution1{
    public ListNode reverseBetween(ListNode head, int left, int right) {
        if(left == right){
            return head;
        }
        ListNode leftnode = head;
        ListNode leftprev = head;
        int size = right - left;
        if(left == 1){
            ListNode next = head.next;
            ListNode cur = head;
            while(size != 0){
                ListNode nextnext = next.next;
                next.next = cur;
                head = next;
                cur = head;
                next = nextnext;
                size--;
            }
            leftnode.next = next;
        }else{
            int length = 1;
            while(length != left){
                leftprev = leftnode;
                leftnode = leftnode.next;
                length++;
            }
            ListNode next = leftnode.next;
            ListNode cur = leftnode;
            ListNode tmp = leftnode;
            while(size != 0){
                ListNode nextnext = next.next;
                next.next = cur;
                leftnode = next;
                cur = leftnode;
                next = nextnext;
                size--;
            }
            leftprev.next = leftnode;
            tmp.next = next;
        }
        return head;
    }
}


class Solution {

    public int distinctAverages(int[] nums) {
        Set<Float> set = new HashSet<>();
        int[] arr = new int[101];
        for(int i = 0; i < nums.length; i++){
            arr[nums[i]]++;
        }
        int left = 0;
        int right = 100;
        while(left <= right){
            while(left <= right && arr[left] == 0){
                left++;
            }
            while(left <= right && arr[right] == 0){
                right--;
            }
            if(right >= left && arr[right] != 0 && arr[left] != 0){//9,5,7,8,7,9,8,2,0,7
                arr[left]--;
                arr[right]--;
                float avg = (float)((left + right) / 2.0);
                set.add(avg);
            }
        }
        return set.size();
    }

    public ListNode my_addTwoNumbers(ListNode l1, ListNode l2) {
        StringBuffer str1 = new StringBuffer();
        ListNode node = l1;
        while(node != null){
            str1.append(node.val);
            node = node.next;
        }
        StringBuffer str2 = new StringBuffer();
        node = l2;
        while(node != null){
            str2.append(node.val);
            node = node.next;
        }
        StringBuffer str3 = new StringBuffer();
        str3 = add(str1,str2);
        ListNode l3 =  creatList(str3);
        return l3;
    }

    private ListNode creatList(StringBuffer str){
        ListNode head = new ListNode();
        ListNode cur = head;
        int size = str.length();
        int length = 0;
        while(length != size){
            ListNode node = new ListNode(str.charAt(length) - '0');
            cur.next = node;
            cur = node;
            length++;
        }
        return head.next;
    }

    private  StringBuffer add(StringBuffer str1, StringBuffer str2){
        str1 = str1.reverse();
        str2 = str2.reverse();
        StringBuffer str3 = new StringBuffer();
        int length1 = str1.length();
        int length2 = str2.length();
        int min = length1 > length2 ? length2 : length1;
        int flag = 0;
        int num = 0;
        while(flag != min){
            int s1 = str1.charAt(flag) - '0';
            int s2 = str2.charAt(flag) - '0';
            int s3 = num;
            num =  (num + s1 + s2) % 10;
            str3.append(num);
            num = (s1 + s2 + s3) / 10;
            flag++;
        }
        if(length1 == length2 && num != 0){
            str3.append(num);
            return str3.reverse();
        }
        while(flag < length1){
            int s1 = str1.charAt(flag) - '0';
            int s2 = num;
            num = (num + s1) % 10;
            str3.append(num);
            num = (s1 + s2) / 10;
            flag++;
        }
        while(flag < length2){
            int s2 = str2.charAt(flag) - '0';
            int s1 = num;
            num = (num + s2) % 10;
            str3.append(num);
            num = (s2 + s1) / 10;
            flag++;
        }
        if(num != 0){
            str3.append(num);
        }
        return str3.reverse();
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] arr = {9,5,7,8,7,9,8,2,0,7};
        int n = solution.distinctAverages(arr);
        System.out.println(n);
    }
}

public class Test {

    public Queue<Integer> queue1 = new LinkedList<>();
    public Queue<Integer> queue2 = new LinkedList<>();

    public boolean push(int val){
        if(queue1.isEmpty() && queue2.isEmpty()){
            queue1.add(val);
        }else if(queue1.isEmpty()){
            queue2.add(val);
        }else{
            queue1.add(val);
        }
        return true;
    }

    public int pop(){
        if(queue1.isEmpty()){
            int size = queue2.size();
            for(int i = 0; i < size - 1; i++){
                int del = queue2.poll();
                queue1.add(del);
            }
            return queue2.poll();
        }else {
            int size = queue1.size();
            for(int i = 0; i < size - 1; i++){
                int del = queue1.poll();
                queue2.add(del);
            }
            return queue1.poll();
        }
    }

    public ListNode mergeKLists(ListNode[] lists) {
        ListNode head = null;
        for(int i = 1; i < lists.length; i++){
            head = mergeKList(lists[0],lists[i]);
        }
        return head;
    }
    private ListNode mergeKList(ListNode l1, ListNode l2){
        ListNode head = new ListNode();
        ListNode cur = head;
        while(l1 != null && l2 != null){
            int n1 = l1.val;
            int n2 = l2.val;
            if(n1 < n2){
                cur.next = l1;
                cur = l1;
            }else{
                cur.next = l2;
                cur = l2;
            }
            l1 = l1.next;
            l2 = l2.next;
        }
        if(l1 != null){
            cur.next = l1;
        }else{
            cur.next = l2;
        }
        return head.next;
    }

    public static void main(String[] args) {

//        List<Integer> list = new ArrayList<>();
//        Stack<Character> stack = new Stack<>();
//        String str = "dsca";
//        stack.peek('a');
//        stack.contains('a');;
//        //
//        stack.pop();
//        stack.push();//
//        StringBuffer stringBuffer = new StringBuffer();
//        stringBuffer.reverse();
//        stringBuffer
    }
}
